﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.esriSystem;
using MidTermWorkWPF.MyCommands;
using MidTermWorkWPF.MyTools;

namespace MidTermWorkWPF
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        AxMapControl mapControl = null;
        AxTOCControl tocControl = null;
        AxToolbarControl toolbarControl = null;

        ILayer pLayer = null;
        public MainWindow()
        {
            InitializeComponent();
            CreateEngineControls();
        }

        //建立AE控件与WindowsFormsHost控件的联系
        private void CreateEngineControls()
        {
            mapControl = new AxMapControl();
            ForMapControl.Child = mapControl;
            tocControl = new AxTOCControl();
            ForTOCControl.Child = tocControl;
            toolbarControl = new AxToolbarControl();
            ForToolBar.Child = toolbarControl;
        }

        //ToolBar中添加工具
        private void ToolbarAddItems()
        {
            toolbarControl.AddItem(new ControlsOpenDocCommandClass(), -1, -1, false, -1, esriCommandStyles.esriCommandStyleIconOnly);
            toolbarControl.AddItem(new ControlsSaveAsDocCommandClass(), -1, -1, false, -1, esriCommandStyles.esriCommandStyleIconOnly);
            toolbarControl.AddItem(new ControlsAddDataCommandClass(), -1, -1, false, -1, esriCommandStyles.esriCommandStyleIconOnly);
            toolbarControl.AddItem("esriControls.ControlsMapNavigationToolbar");
            toolbarControl.AddItem("esriControls.ControlsMapIdentifyTool");
            toolbarControl.AddItem(new ControlsSelectToolClass(), -1, -1, false, -1, esriCommandStyles.esriCommandStyleIconOnly);
            toolbarControl.AddItem(new MyZoomCommandClass(), -1, -1, false, -1, esriCommandStyles.esriCommandStyleIconOnly);
            toolbarControl.AddItem(new MyAddDateToolClass(), -1, -1, false, -1, esriCommandStyles.esriCommandStyleIconOnly);
        }

        //处理点击图层控件事件的逻辑
        private void TOCControl_MouseDown(object sender, ESRI.ArcGIS.Controls.ITOCControlEvents_OnMouseDownEvent e)
        {
            esriTOCControlItem item = new esriTOCControlItem();
            IBasicMap map = new Map() as IBasicMap;
            object o = new object();
            tocControl.HitTest(e.x, e.y, ref item, ref map, ref pLayer, ref o, ref o);

            IToolbarMenu mapPopMenu = null;
            mapPopMenu = new ToolbarMenu();//这个很关键，主要应用SDK封装的工具类似于C#的OpenDialog
            if (e.button == 2 && map != null && pLayer == null)//右击到地图
            {
                mapPopMenu.AddItem(new ControlsAddDataCommand(), -1, 0, false, esriCommandStyles.esriCommandStyleIconAndText);
                mapPopMenu.AddItem(new MyLayerUnvisibleCommandClass(), -1, 1, true, esriCommandStyles.esriCommandStyleIconAndText);
                mapPopMenu.AddItem(new MyLayerVisibleCommandClass(), -1, 2, false, esriCommandStyles.esriCommandStyleIconAndText);
            }
            else if (e.button == 2 && map != null && pLayer != null && !(pLayer is ICompositeLayer))//右击到图层
            {
                mapPopMenu.AddItem(new MyOpenLayerAttributeTableCommandClass(pLayer, mapControl.Map), -1, 0, false, esriCommandStyles.esriCommandStyleIconAndText);
                mapPopMenu.AddItem(new MyDeleteLayerCommandClass(pLayer), -1, 1, false, esriCommandStyles.esriCommandStyleIconAndText);
                mapPopMenu.AddItem(new MySetMaxScaleCommandClass(pLayer, (long)mapControl.MapScale), -1, 2, true, esriCommandStyles.esriCommandStyleIconAndText);
                mapPopMenu.AddItem(new MySetMinScaleCommandClass(pLayer, (long)mapControl.MapScale), -1, 3, false, esriCommandStyles.esriCommandStyleIconAndText);
                mapPopMenu.AddItem(new MyRemoveScaleThresholdsCommandsClass(pLayer), -1, 4, false, esriCommandStyles.esriCommandStyleIconAndText);
                mapPopMenu.AddItem(new MyZoomToLayerCommandClass(pLayer), -1, 5, true, esriCommandStyles.esriCommandStyleIconAndText);
            }
            mapPopMenu.SetHook(mapControl); //得到地图视窗右键菜单
            mapPopMenu.PopupMenu(e.x, e.y, tocControl.hWnd);//弹出显示菜单
        }

        //处理鼠标移动事件（刷新比例尺和坐标）
        private void mapControl_OnMouseMove(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseMoveEvent e)
        {
            //当前比例尺
            Scale.Text = "1:" + ((long)mapControl.MapScale).ToString();
            IProjectedCoordinateSystem pcs = this.mapControl.SpatialReference as IProjectedCoordinateSystem;
            //定义点
            WKSPoint pt = new WKSPoint(); //不能用IPoint pt = new PointClass();因为后面的方法只支持WKSPoint
            pt.X = e.mapX;
            pt.Y = e.mapY;
            try
            {
                CoordinateX.Text = pt.X.ToString();
                CoordinateY.Text = pt.Y.ToString();
            }
            catch (Exception)
            {
                CoordinateX.Text = "0";
                CoordinateY.Text = "0";
            }

            //原计划将其转化为地理坐标系，但由于测试数据等问题没能实现
            //double d1, n1, m1, L1, d2, n2, m2, L2;
            //pcs.Inverse(1, ref pt); //将平面坐标转换为地理坐标                 
            //d1 = pt.X / 1;//度的整数部分                 
            //n1 = pt.X % 1;//度的小数部分                 
            //m1 = (n1 * 60) / 1;//分                 
            //L1 = ((n1 * 60) % 1) * 60;//秒                 
            //d2 = pt.Y / 1;//纬度的整数部分                 
            //n2 = pt.Y % 1;//度的小数部分                 
            //m2 = (n2 * 60) / 1;//分  
            //string m22;
            //if (m2 < 10)
            //    m22 = "0" + m2.ToString();
            //else
            //    m22 = m2.ToString();
            //L2 = ((n2 * 60) % 1) * 60;//秒 
            //try
            //{
            //    CoordinateX.Text = d1.ToString().Remove(3) + "°" + m1.ToString().Remove(2) + " '" + L1.ToString().Remove(6) + "\"" + "E ";
            //    CoordinateY.Text = d2.ToString().Remove(2) + "°" + m22.Remove(2) + " '" + L2.ToString().Remove(6) + "\"" + "N ";
            //}
            //catch (Exception)
            //{

            //    CoordinateX.Text = "0";
            //    CoordinateY.Text = "0";
            //}
        }

        //窗口加载事件
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            tocControl.SetBuddyControl(mapControl);
            toolbarControl.SetBuddyControl(mapControl);
            ToolbarAddItems();
            // 注册点击事件处理程序
            tocControl.OnMouseDown += new ITOCControlEvents_Ax_OnMouseDownEventHandler(TOCControl_MouseDown);
            mapControl.OnMouseMove += new IMapControlEvents2_Ax_OnMouseMoveEventHandler(mapControl_OnMouseMove);
        }
    }
}
